#!/bin/bash

OK=0
WARN=1
CRITICAL=2
UNKNOWN=3
PATH="/bin:/sbin:/usr/bin:/usr/sbin"

Warn_temp=40
Shutdown_temp=50
Expired_time=900 #second 15min

cd /usr/lib64/nagios/plugins/

#`/usr//bin/wget http://scc.ustc.edu.cn/temp/?lcp=E1 -O /tmp/E1.temp`   run on xman server,every 5min
hostname_1=`/bin/hostname -s`
ip_addr=`/sbin/ifconfig|sed -n '/inet addr/s/^[^:]*:\([0-9.]\{7,15\}\) .*/\1/p' |grep -v 127.0.0.1 |grep -v 202`
#aircondition_sensor=`grep $hostname_1 air-condition-temp-location.list  |awk -F "LCP-" '{print $NF}'`  #equal G3 or G4
#echo $ip_addr
aircondition_sensor=`grep $ip_addr air-condition-temp-location.list|sort -n -k 2 |head -n 1|awk -F "LCP-" '{print $NF}'`  #equal G3 or G4
#echo $aircondition_sensor



if [ -e /software/nagios/$aircondition_sensor.temp ]
then

   Front_Temp=`cat /software/nagios/$aircondition_sensor.temp  |awk -F '|' '{print $2}'`
   Back_Temp=`cat /software/nagios/$aircondition_sensor.temp  |awk -F '|' '{print $2}'`

   Fetch_Time=`cat /software/nagios/$aircondition_sensor.temp  |awk -F '|' '{print $NF}'`
   Fetch_Time11=`cat /software/nagios/$aircondition_sensor.temp`
   Now_time=`date +%s`
   Fetch_time1=$(date +%s -d "$Fetch_Time")
   Time_Cha=$(($Now_time-$Fetch_time1))

   if [[ $Time_Cha -gt $Expired_time ]];then    #bigger than  15min

           echo "11Fetch_Time:$Fetch_Time11,Fetch_Time:$Fetch_Time,Fetch_Time1:$Fetch_Time1,Now_time:$Now_time,Time_Cha:$Time_Cha ,Air  temperature expired 900s"
	   /bin/cp /software/nagios/$aircondition_sensor.temp /tmp/test.$Now_time
           exit $CRITICAL
   else
           if [ $Back_Temp -gt $Shutdown_temp ];then
                   echo "Air conditioning back temp now is $Back_Temp exceed Shutdown_temp $Shutdown_temp, exec shutdown command|BackTemp=$Back_Temp;$Warn_temp;$Shutdown_temp;;"
                   echo "o" > /proc/sysrq-trigger   ####exec shutdown command
                   exit $CRITICAL
           elif [ $Back_Temp -gt $Warn_temp ];then
                   echo "Air conditioning back temp now is $Back_Temp exceed warning temp $Warn_temp but lower than Shutdown_temp $Shutdown_temp|BackTemp=$Back_Temp;$Warn_temp;$Shutdown_temp;;"
                   exit $WARN
           else
                   echo "Air conditioning back temp now is $Back_Temp lower than warning temp $Warn_temp and Shutdown_temp $Shutdown_temp|BackTemp=$Back_Temp;$Warn_temp;$Shutdown_temp;;"
                   exit $OK
           fi
   fi
else
   echo "/software/nagios/$aircondition_sensor.temp does not exist"
   exit $CRITICAL
fi
